#include "topconn.ch"
#INCLUDE "rwmake.ch"
/*#########################################################
# Programa   : GSquota.PRW            DATA: 20/02/2008    #
# Modulo     : Faturamento                                #
# Objetivo   : Calcula a quantidade vendida e produto     #
#            : e valorizar o saldo disponivel vendedor    #
# Programador: Wanderson F.Souza/Matheus Rodrigues/Antonio#
#########################################################*/

User Function AVI00026()
Private lEnd       := .F.
Private lAbortPrint:= .F.
Private CbTxt      := ""
Private limite     := 080
Private tamanho    := "P"
Private nomeprog   := "AVI00026"
Private nTipo      := 18
Private aReturn    := { "Zebrado", 1, "Administracao", 2, 2, 1, "", 1}
Private nLastKey   := 0
Private cPerg      := "GSQUOTA5"
Private cbtxt      := Space(10)
Private cbcont     := 00
Private CONTFL     := 01
Private m_pag      := 01
Private wnrel      := "AVI00026"
Private cString := "SCK"
ValidPerg(cPerg)
aMatriz:={};nPos:=0
nLin         := 0
titulo       := "Processamento da cota do vendedor           "
cDesc1       := "Este programa tem como objetivo imprimir relatorio  "
cDesc2       := "de acordo com os parametros informados pelo usuario."
cDesc3       := ""
cPict        := ""
Cabec1       := "Goncalves Salles S/A Industria e Comercio"
Cabec2       := ""
imprime      := .T.
aOrd 	     := {}
nValor:=0
dbSelectArea("SZP")
dbSetOrder(1)
pergunte(cPerg,.F.)

wnrel := SetPrint(cString,NomeProg,cPerg,@titulo,cDesc1,cDesc2,cDesc3,.T.,aOrd,.T.,Tamanho,,.T.)
If nLastKey == 27
	Return
Endif
SetDefault(aReturn,cString)
If nLastKey == 27
	Return
Endif
RptStatus({|| RunReport() },Titulo)
Return

****************************************************
Static Function RunReport()
Local _cQrySE1:=""
SetRegua(5000)
Pergunte(cPerg,.f.)
DbSelectArea("SZP")

_cQrySE1 := "SELECT SZP.ZP_PRODUTO AS CODIGO,SZP.ZP_VENDEDO AS VENDEDOR,SZP.ZP_CAIXAS AS COTA, SZP.ZP_SALDO AS SALDO"
_cQrySE1 += " FROM SZP"+cEmpAnt+"0 SZP"
_cQrySE1 += " WHERE SZP.ZP_FILIAL = '"+xFilial("SZP")+"'"
_cQrySE1 += " AND SZP.D_E_L_E_T_ <> '*'"
_cQrySE1 += " AND SZP.ZP_VENDEDO    BETWEEN '"+mv_par01+"' AND '"+mv_par02+"'"
_cQrySE1 += " AND SZP.ZP_PRODUTO BETWEEN '"+mv_par03+"' AND '"+mv_par04+"'"
If Select("SE1SQL") > 0
	SE1SQL->(DbCloseArea())
Endif
TCQUERY _cQrySE1 NEW ALIAS "SE1SQL"
TCSetField("SE1SQL","CODIGO","C",6)
TCSetField("SE1SQL","VENDEDOR","C",3)
TCSetField("SE1SQL","NOME","C",30,0)
TCSetField("SE1SQL","SALDO","N",14,2)
TCSetField("SE1SQL","CODTA","N",14,2)

SE1SQL->(DbGoTop())

Do While SE1SQL->(!Eof())
			RecLock("SZP",.F.)
			Replace SE1SQL->ZP_SALDO  With SE1SQL->ZP_CAIXAS
			MsUnLock()
			DbSkip()
			Loop
		Enddo
		DbSelectArea("SE1SQL")
		SE1SQL->(DbSkip())
//	Enddo
//Enddo
U_GSCOTAI()
Return


*****************
Static Function ValidPerg(cPerg)

Local _sAlias := Alias()
Local aRegs := {}
Local i,j

dbSelectArea("SX1")
dbSetOrder(1)
cPerg := PADR(cPerg,6)
Aadd(aRegs,{cPerg,"01","Vendedor de   ?","","","mv_ch1","C",03,0,0,"G","","mv_par01","","","","","","","","","","","","","","","","","","","","","","","","","","",""})
Aadd(aRegs,{cPerg,"02","Vendedor ate  ?","","","mv_ch2","C",03,0,0,"G","","mv_par02","","","","","","","","","","","","","","","","","","","","","","","","","","",""})
Aadd(aRegs,{cPerg,"03","Produto de    ?","","","mv_ch3","C",06,0,0,"G","","mv_par03","","","","","","","","","","","","","","","","","","","","","","","","","","",""})
Aadd(aRegs,{cPerg,"04","Produto ate   ?","","","mv_ch4","C",06,0,0,"G","","mv_par04","","","","","","","","","","","","","","","","","","","","","","","","","","",""})
For i:=1 to Len(aRegs)
	If !dbSeek(cPerg+aRegs[i,2])
		RecLock("SX1",.T.)
		For j:=1 to FCount()
			If j <= Len(aRegs[i])
				FieldPut(j,aRegs[i,j])
			EndIf
		Next
		MsUnlock()
	EndIf
Next
dbSelectArea(_sAlias)
Return

